<%@page import="com.sinba.common.system.service.ITransactionRecordService"%>
<%@page import="com.sinba.common.business.service.IBuyService"%>
<%@page import="com.sinba.common.business.dto.BuyQueryDTO"%>
<%@page import="com.sinba.common.dto.PageDTO"%>
<%@page import="com.sinba.common.user.dto.AccountDTO"%>
<%@page import="com.sinba.common.business.service.BuyService"%>
<%@page import="com.sinba.common.user.service.IAccountService"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> 
<%@ include file="../language.jsp" %>
<%
session.setAttribute("middle", "交易管理");
session.setAttribute("end", "基本信息");
if(session.getAttribute("password2")==null){
	/* response.sendRedirect( "login2.jsp?pageUrl=user/buy.jsp");
	return; */
}

ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
IBuyService buyService = (IBuyService)ac.getBean("buyService");
BuyQueryDTO qdto = new BuyQueryDTO();
qdto.setBuyUser((String)session.getAttribute("loginName"));
qdto.setSearchType("BuyAndSell");
int pages = 1;
String sidx = "createTime";
String sord="desc";
if(request.getParameter("pages")!=null && request.getParameter("_pages")!=""){
	pages = Integer.parseInt(request.getParameter("pages"));
}
int rows = 2;
int start = ( pages - 1 ) * rows;
qdto.setStart( start );
qdto.setLimit( rows );
PageDTO pageDTO = buyService.findBuyPage(qdto, sord, sidx);
if(pageDTO!=null){
	pageDTO.setPage( pages );
	pageDTO.setRows( rows );
}
request.setAttribute("pageDTO",pageDTO);

ITransactionRecordService transactionRecordService = (ITransactionRecordService)ac.getBean("transactionRecordService");
Double balance = transactionRecordService.findUserBalance(session.getAttribute("loginName").toString(),"Static");
request.setAttribute("balance",balance);

request.setAttribute("pageUrl","buy.jsp?loginName="+ qdto.getBuyUser());
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title><fmt:message key="i18n.accountZhuanZhang"/>-<fmt:message key="i18n.title"/> </title>
<jsp:include page="common.jsp"></jsp:include>
</head>
<body >
	<jsp:include page="page_head.jsp"></jsp:include>
	
	<div class="row">
	<div class="col-md-12">
		<div class="box border primary">
			<div class="box-title">
				<h4><i class="fa fa-bars"></i>买入</h4>
			</div>
			<div class="box-body big">
				<form  id="buy_form" class="form-horizontal" role="form" action="buy!buy.action" method="post">
		      	<input type="hidden" name="pageUrl" value="buy.jsp" />
		      	<input type="hidden" name="buyDTO.buyUser" value="${session.loginName }" />
				  <div class="form-group">
					<label class="col-sm-3 control-label"><fmt:message key="i18n.money"/>：</label>
					<div class="col-sm-9">
					  <input type="text" class="form-control" name="buyDTO.dBuyMoney" id="money" readonly="readonly" value="${balance}">
					</div>
				  </div>
				  <div class="form-group">
					<div class="col-sm-offset-3 col-sm-10">
					  <button type="submit" class="btn btn-pink"><fmt:message key="i18n.aKeyAutoBuy" /></button>
					</div>
				  </div>
			  </div>
			 </form>
			</div>
		</div>
		</div>
		<!-- ROW 3 -->
		<div class="row">
			<div class="col-md-12">
				<div class="box border primary">
					<div class="box-title">
						<h4>
							<i class="fa fa-columns"></i> <span
								class="hidden-inline-mobile"><span>卖出/</span>买入</span>
						</h4>
					</div>
					<div class="box-body">
						<div class="tabbable">
							<ul class="nav nav-tabs">
								<li class="active"><a href="#sell"
									data-toggle="tab"><i class="fa fa-signal"></i> <span
										class="hidden-inline-mobile">卖出</span></a></li>
								<li><a href="#buy" data-toggle="tab"><i
										class="fa fa-rss"></i> <span
										class="hidden-inline-mobile">买入</span></a></li>
							</ul>
							<div class="tab-content">
								<div class="tab-pane active" id="sell">
									<table
										class="table table-striped table-bordered table-hover">
										<thead>
											<tr>
												<th>买入时间</th>
									        	<th>买入金额</th>
									        	<th>买入价位</th>
									        	<th>持有股数</th>
									        	<th>现持有股数</th>
											</tr>
										</thead>
										<tbody>
											<c:forEach items="${pageDTO.data}" var="data">
									  			<tr>
													<%-- <td>${data.id }</td> --%>
													<%-- <td>${data.buyUser }</td> --%>
													<td><fmt:formatDate value="${data.createTime }" pattern="yyMMdd HH:mm:ss"/></td>
													<td>${data.dBuyMoney }</td>
													<td>${data.buyPrices }</td>
													<td>${data.holdStock }</td>
													<td>
														<c:set var="sum" value="0"></c:set> 
														<c:if test="${fn:length(data.sellGrids) ne 0}">
															<c:forEach items="${data.sellGrids}" var="sellData">
																<c:if test="${sellData.status ne 2 }" >
																	<c:set value="${sum+sellData.sellMoney}" var="sum"/> 
																</c:if>
															</c:forEach>	
														</c:if>
														${sum}
													</td>
												</tr>
											</c:forEach>
											<c:if test="${empty pageDTO.data}">
													<tr>
														<td colspan="5" align="center">没有数据</td>
													</tr>
											</c:if>
											<tr>
													<td colspan="5" style="text-align: right;">
													<a style="color: blue;" href="${pageUrl }&pages=1"><fmt:message key="i18n.firstPage"/></a>
													<c:if test="${pageDTO.page > 1}">
														<a style="color: blue;" href="${pageUrl }&pages=${pageDTO.page-1}"><fmt:message key="i18n.upPage"/></a>
													</c:if>
													<a>${pageDTO.page}/${pageDTO.total}</a>
													<c:if test="${pageDTO.page+1<=pageDTO.total}">
														<a style="color: blue;" href="${pageUrl }&pages=${pageDTO.page+1}"><fmt:message key="i18n.nextPage"/></a>
													</c:if>
													<a style="color: blue;" href="${pageUrl }&pages=${pageDTO.total}"><fmt:message key="i18n.endPage"/></a>
													<a><fmt:message key="i18n.total"/>${pageDTO.totalSize}<fmt:message key="i18n.tiao"/></a>
													</td>
												</tr>
										</tbody>
									</table>
								</div>
								<div class="tab-pane" id="buy">
									<div class="scroller" data-height="250px"
										data-always-visible="1" data-rail-visible="1">
										<table
											class="table table-striped table-bordered table-hover">
											<thead>
												<tr>
									        		<th>买入状态</th>
										        	<th>波数</th>
										        	<th>卖出股数</th>
										        	<th>卖出价位</th>
										        	<th>已卖出股数</th>
										        	<th>拆分后股数</th>
										        	<th>拆分前股数</th>
										        	<th>拆分倍数</th>
										        	<th>拆分次数</th>
												</tr>
											</thead>
											<tbody>
												<c:forEach items="${pageDTO.data}" var="data">
									  				<tr>
													<td>
													<c:choose>
													   <c:when test="${data.status eq 1}"> 
														   <span style="color:#008AD4;">排队购股</span>
													   </c:when>
													   <c:when test="${data.status eq 2}"> 
														    <span style="color:green;">购股成功</span>
													   </c:when>
													   <c:when test="${data.status eq 3}"> 
													   		<span style="color:red;"><fmt:message key="i18n.cleaned" /></span>
													   </c:when>
													   <c:otherwise>
														   <span style="color:blue;">排队购股</span>
													   </c:otherwise>
													</c:choose>
													</td>
													<td colspan="8" style="padding: 0px; margin: 0px;">
														<c:if test="${fn:length(data.sellGrids) ne 0}">
														<table width="100%" >
															<c:forEach items="${data.sellGrids}" var="sellData">
																<tr>
																	<td class="subtd <c:if test="${sellData.status eq 2 }">subtd_complete</c:if>">${sellData.remark }</td>
																	<td class="subtd <c:if test="${sellData.status eq 2 }">subtd_complete</c:if>">${sellData.sellMoney }</td>
																	<td class="subtd <c:if test="${sellData.status eq 2 }">subtd_complete</c:if>">${sellData.sellPrice }</td>
																	<td class="subtd <c:if test="${sellData.status eq 2 }">subtd_complete</c:if>">${sellData.sellMoney-sellData.surplusMoney }</td>
																	<td class="subtd <c:if test="${sellData.status eq 2 }">subtd_complete</c:if>">${sellData.splitedGuShu }</td>
																	<td class="subtd <c:if test="${sellData.status eq 2 }">subtd_complete</c:if>">${sellData.splitedGuShu/2 }</td>
																	<td class="subtd <c:if test="${sellData.status eq 2 }">subtd_complete</c:if>">2</td>
																	<td class="subtd <c:if test="${sellData.status eq 2 }">subtd_complete</c:if>">${sellData.splitQuy }</td>
																</tr>
															</c:forEach>
														</table>
														</c:if>
														<c:if test="${data.status eq 2 and fn:length(data.sellGrids) eq 0}"> 
																已合并至同价位数据中!
														</c:if>
													</td>
													
													<%-- <td>
													<c:if test="${data.status ne 3}">${data.queuingDays}</c:if> </td>
													<td>
													<c:if test="${data.status ne 3}">
													<c:if test="${data.queuingDays > 7}">7</c:if><c:if test="${data.queuingDays <= 7}">${data.queuingDays}</c:if>
													</c:if></td>
													<td>
														<c:if test="${data.status ne 3}">2%</c:if>
													</td>
													<td><c:if test="${data.status ne 3}">7</c:if></td>
													<td>
														<c:if test="${data.status ne 3}">
															<c:if test="${data.queuingDays > 7}">${6*data.buyMoney*0.02}</c:if>
															<c:if test="${data.queuingDays <= 7}">${data.queuingDays*data.buyMoney*0.02}</c:if>
														</c:if>
													</td>
													<td>
														<c:if test="${data.status eq 0}">
															<a href="javascript:buyClean('${data.id}')">[<fmt:message key="i18n.cancel" />]</a>
														</c:if>
														<c:if test="${data.status eq 1 or data.status eq 2}">
															<a href="matchBuy.jsp">[<fmt:message key="i18n.detail" />]</a>
														</c:if>
														
													</td> --%>
									  			</tr>
									   			</c:forEach>
									   			<c:if test="${empty pageDTO.data}">
													<tr>
														<td colspan="9" align="center">没有数据</td>
													</tr>
												</c:if>
												
												<tr>
													<td colspan="9" style="text-align: right;">
													<a style="color: blue;" href="${pageUrl }&pages=1"><fmt:message key="i18n.firstPage"/></a>
													<c:if test="${pageDTO.page > 1}">
														<a style="color: blue;" href="${pageUrl }&pages=${pageDTO.page-1}"><fmt:message key="i18n.upPage"/></a>
													</c:if>
													<a>${pageDTO.page}/${pageDTO.total}</a>
													<c:if test="${pageDTO.page+1<=pageDTO.total}">
														<a style="color: blue;" href="${pageUrl }&pages=${pageDTO.page+1}"><fmt:message key="i18n.nextPage"/></a>
													</c:if>
													<a style="color: blue;" href="${pageUrl }&pages=${pageDTO.total}"><fmt:message key="i18n.endPage"/></a>
													<a><fmt:message key="i18n.total"/>${pageDTO.totalSize}<fmt:message key="i18n.tiao"/></a>
													</td>
												</tr>
											</tbody>
										</table>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<!-- /ROW3 -->
	<jsp:include page="page_foot.jsp"></jsp:include>
<script>
	$(function(){
		$("#buy_form").submit(function(){
			var money = $("#money").val();
			if(isNaN(money)){
				alert('请输入正确的数值！');
			}else{
				/*  var pattern = /^[1-9]\d*000$/; 
				 if(pattern.test(money)){
					 if(parseInt(money)>50000){
						 alert('金额不能超过50000');
					 }else if(parseInt(money)<2000){
						 alert('金额不能少于2000');
					 }else{
						 return true;
					 }
				 }else{
					 alert("请输入1000的倍数");
				 } */
				return true;
			}
			return false;
		});
	});


function findUserNameByLoginName(loginName){
	$.post('registerUser!findUserNameByLoginName.action','registerUserDTO.loginName='+$('#transfer_transAccount').val(),function(res){
		$('#transfer_userName').text(res);
	});	
}

function checkform(){
	if($('#transfer_transAccount').val()==''){
		alert('Into account cannot be empty(转入账号不能为空)!');
		$('#transfer_transAccount').focus();
		return false;
	}
	if($('#transfer_value').val()==''){
		alert('Transferred to the amount can not be empty(转入金额不能为空)!');
		$('#transfer_value').focus();
		return false;
	}
	var toMoeny = $('#transfer_value').val()
	if(toMoeny!=''){
		toMoeny = toMoeny.replace(/(^\s*)|(\s*$)/g, ""); 
	}
	var a = /^(\d*|\-?[1-9]{1}\d*)$/;
	if(!toMoeny.match(a) || toMoeny==0){
		alert("Transferred to the amount of not positive integer(转入金额不是正整数)!");
		return false;
	}
	if(toMoeny % 10 != 0){
		alert('Please enter 10 times the amount(请输入10的倍数金额)!');
		return false;
	}
	if($('#transfer_validCode').val()==''){
		alert('Verification code cannot be empty(验证码不能为空)!');
		$('#transfer_validCode').focus();
		return false;
	}
	if($('#transfer_passwordAnswer').val()==''){
		alert('Secret security answer can not be empty(密保答案不能为空)!');
		$('#transfer_passwordAnswer').focus();
		return false;
	}
	
	$('#transfer_form').submit();
	/**
	$.post('activationCode!authEmailCode.action','dto.type=邮件&dto.category=转账&dto.activationCode='+$('#transfer_validCode').val(),function(res){
		 if(!res){
			 alert('邮箱验证码不正确!');
			 return false;
		 }else{
			 $('#transfer_form').submit();
		 }
	});
	**/
	}

	function buyClean(buyId){
		if(confirm('是否确定要取消，取消后日利息系统会自动扣除?')){
			$('#buyId').val(buyId);
			$('#buyCleanForm').submit();
		}
	}




</script>	
</body>
</html>